package com.jinlejia.erp.ui.house;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.jinlejia.erp.R;
import com.jinlejia.erp.databinding.FragmentInformationBinding;

import org.simple.eventbus.EventBus;
import org.simple.eventbus.Subscriber;

import java.util.ArrayList;
import java.util.Map;

public class InformationFragment extends Fragment {

    public InformationFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
        }
    }
    private FragmentInformationBinding binding;
    private View view;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_information, container, false);
        binding = DataBindingUtil.bind(view);
        EventBus.getDefault().register(this);
        return view;
    }

    @Subscriber(tag = "TAG_HOUSE_DATA")
    public void houseData(JSONObject data) {
        parseData(data);
    }
    private int[]layouts = new int[]{R.id.layout_1,R.id.layout_2,R.id.layout_3,R.id.layout_4,R.id.layout_5,
            R.id.layout_6,R.id.layout_7,R.id.layout_8,R.id.layout_9,R.id.layout_10,
            R.id.layout_11,R.id.layout_12,R.id.layout_13,R.id.layout_14,R.id.layout_15,
            R.id.layout_16,R.id.layout_17,R.id.layout_18,R.id.layout_19,R.id.layout_20,
            R.id.layout_21,R.id.layout_22,R.id.layout_23,R.id.layout_24,R.id.layout_25,
            R.id.layout_26,R.id.layout_27,R.id.layout_28,R.id.layout_29,R.id.layout_30,
            R.id.layout_31,R.id.layout_32,R.id.layout_33,R.id.layout_34,R.id.layout_35};
    private int[]names = new int[]{R.id.name_1,R.id.name_2,R.id.name_3,R.id.name_4,R.id.name_5,
            R.id.name_6,R.id.name_7,R.id.name_8,R.id.name_9,R.id.name_10,
            R.id.name_11,R.id.name_12,R.id.name_13,R.id.name_14,R.id.name_15,
            R.id.name_16,R.id.name_17,R.id.name_18,R.id.name_19,R.id.name_20,
            R.id.name_21,R.id.name_22,R.id.name_23,R.id.name_24,R.id.name_25,
            R.id.name_26,R.id.name_27,R.id.name_28,R.id.name_29,R.id.name_30,
            R.id.name_31,R.id.name_32,R.id.name_33,R.id.name_34,R.id.name_35};
    private int[]areas = new int[]{R.id.area_1,R.id.area_2,R.id.area_3,R.id.area_4,R.id.area_5,
            R.id.area_6,R.id.area_7,R.id.area_8,R.id.area_9,R.id.area_10,
            R.id.area_11,R.id.area_12,R.id.area_13,R.id.area_14,R.id.area_15,
            R.id.area_16,R.id.area_17,R.id.area_18,R.id.area_19,R.id.area_20,
            R.id.area_21,R.id.area_22,R.id.area_23,R.id.area_24,R.id.area_25,
            R.id.area_26,R.id.area_27,R.id.area_28,R.id.area_29,R.id.area_30,
            R.id.area_31,R.id.area_32,R.id.area_33,R.id.area_34,R.id.area_35};
    private int[]orientations = new int[]{R.id.orientation_1,R.id.orientation_2,R.id.orientation_3,R.id.orientation_4,R.id.orientation_5,
            R.id.orientation_6,R.id.orientation_7,R.id.orientation_8,R.id.orientation_9,R.id.orientation_10,
            R.id.orientation_11,R.id.orientation_12,R.id.orientation_13,R.id.orientation_14,R.id.orientation_15,
            R.id.orientation_16,R.id.orientation_17,R.id.orientation_18,R.id.orientation_19,R.id.orientation_20,
            R.id.orientation_21,R.id.orientation_22,R.id.orientation_23,R.id.orientation_24,R.id.orientation_25,
            R.id.orientation_26,R.id.orientation_27,R.id.orientation_28,R.id.orientation_29,R.id.orientation_30,
            R.id.orientation_31,R.id.orientation_32,R.id.orientation_33,R.id.orientation_34,R.id.orientation_35};
    private int[]windows = new int[]{R.id.windows_1,R.id.windows_2,R.id.windows_3,R.id.windows_4,R.id.windows_5,
            R.id.windows_6,R.id.windows_7,R.id.windows_8,R.id.windows_9,R.id.windows_10,
            R.id.windows_11,R.id.windows_12,R.id.windows_13,R.id.windows_14,R.id.windows_15,
            R.id.windows_16,R.id.windows_17,R.id.windows_18,R.id.windows_19,R.id.windows_20,
            R.id.windows_21,R.id.windows_22,R.id.windows_23,R.id.windows_24,R.id.windows_25,
            R.id.windows_26,R.id.windows_27,R.id.windows_28,R.id.windows_29,R.id.windows_30,
            R.id.windows_31,R.id.windows_32,R.id.windows_33,R.id.windows_34,R.id.windows_35};
    private void parseData(JSONObject data) {
        if (data==null){
            binding.emptyLayout.setVisibility(View.VISIBLE);
            binding.dataLayout.setVisibility(View.GONE);
            return;
        }

        String name = data.getString("houseType");
        if (!TextUtils.isEmpty(name)){
            String[] split = name.split("-");
            if (split.length>=4){
                name = String.format("%s室%s厅%s卫%s厨",split[0],split[1],split[3],split[2]);
            }
        }
        final ArrayList<Map<String, Object>> mImgList = JSON.parseObject(data.getJSONArray("imgList").toString(), new TypeReference<ArrayList<Map<String, Object>>>() {
        });
        ArrayList<Map<String, Object>> list;
        if (mImgList!=null){
            list = new ArrayList<>(mImgList);
            for (Map<String, Object> map:list) {
                if ("12".equalsIgnoreCase(String.valueOf(map.get("type")))){
                    mImgList.remove(map);
                }
            }
        }
        ArrayList<Map<String, Object>> tempList = mImgList;
        if (tempList==null || tempList.size()==0){
            binding.emptyLayout.setVisibility(View.VISIBLE);
            binding.dataLayout.setVisibility(View.GONE);
            return;
        }
        binding.dataLayout.setVisibility(View.VISIBLE);
        binding.emptyLayout.setVisibility(View.GONE);
        if (name==null){
            binding.tipHouseInfoLayout.setVisibility(View.GONE);
        }else {
            binding.tipHouseInfoLayout.setVisibility(View.VISIBLE);
        }
        binding.tipHouseTextView.setText(name);
        for (int i = 0; i < layouts.length; i++) {
            LinearLayout layout = view.findViewById(layouts[i]);
            if (tempList.size()>=i+1){
                layout.setVisibility(View.VISIBLE);
                TextView nameTextView = view.findViewById(names[i]);
                TextView areaTextView = view.findViewById(areas[i]);
                TextView orientationTextView = view.findViewById(orientations[i]);
                TextView windowsTextView = view.findViewById(windows[i]);
                Map<String, Object> map = tempList.get(i);
                nameTextView.setText(String.valueOf(map.get("name")));
                String suqare = String.valueOf(map.get("suqare"));
                if (TextUtils.isEmpty(suqare)){
                    areaTextView.setText("/");
                }else {
                    double d = 0;
                    try {
                        d = Double.parseDouble(suqare);
                    }catch (Exception ignored){
                    }
                    if (d==0){
                        areaTextView.setText("/");
                    }else {
                        areaTextView.setText(String.valueOf(map.get("suqare")));
                    }
                }
                if (TextUtils.isEmpty(String.valueOf(map.get("face")))){
                    orientationTextView.setText("/");
                }else {
                    orientationTextView.setText(String.valueOf(map.get("face")));
                }
                if (TextUtils.isEmpty(String.valueOf(map.get("windows")))){
                    windowsTextView.setText("/");
                }else {
                    windowsTextView.setText(String.valueOf(map.get("windows")));
                }
            }else {
                layout.setVisibility(View.GONE);
            }


        }
    }

}
